package com.sjyy.common.sdk;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONObject;

import com.google.gson.JsonObject;
import com.sjyy.common.sdk.push.JPush;
import com.sjyy.common.sdk.push.android.AndroidCustomizedcast;
import com.sjyy.common.sdk.push.ios.IOSCustomizedcast;

public class PushSDK {
	private static String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
	
	private static String appkey = "53a66c9c56240bbe5b0eef01";
	private static String masterSecret = "fcphutvuyf2luzx6lct6pisf5zh5mvez";
	private static String validationToken = DigestUtils.md5Hex(appkey.toLowerCase() + masterSecret.toLowerCase() + timestamp);

	private static String appkey_ios = "54aa3f67fd98c5b25e00088e";
	private static String masterSecret_ios = "pqp41zf29qiodm0lbv4negwd7wtrw8on";
	private static String validationToken_ios = DigestUtils.md5Hex(appkey_ios.toLowerCase() + masterSecret_ios.toLowerCase() + timestamp);

	private static String appkey_purchaseApp = "5418fddcfd98c579e904f140";
	private static String masterSecret_purchaseApp = "ktnezftcimzuasnhyckmcevpdjxldtqh";
	private static String validationToken_purchaseApp = DigestUtils.md5Hex(appkey_purchaseApp.toLowerCase() + masterSecret_purchaseApp.toLowerCase() + timestamp);

	public static void sendCommonPush(String telephone, String ticker, String title, String text){
		sendAndroidCustomizedcast(telephone, ticker, title, text);
		sendIOSCustomizedcast(telephone, ticker, title, text);
		//JPush.SendIosPush(telephone, ticker, title, text);
	}
	
	public static void sendAndroidCustomizedcast(String telephone, String ticker, String title, String text) {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast();
		try {
			customizedcast.setPredefinedKeyValue("appkey", appkey);
			customizedcast.setPredefinedKeyValue("timestamp", timestamp);
			customizedcast.setPredefinedKeyValue("validation_token", validationToken);
			// TODO Set your alias here, and use comma to split them if there
			// are multiple alias.
			// And if you have many alias, you can also upload a file containing
			// these alias, then
			// use file_id to send customized notification.
			customizedcast.setPredefinedKeyValue("alias", telephone);
			customizedcast.setPredefinedKeyValue("alias_type", "userId");
			customizedcast.setPredefinedKeyValue("ticker", ticker);
			customizedcast.setPredefinedKeyValue("title", title);
			customizedcast.setPredefinedKeyValue("text", text);
			customizedcast.setPredefinedKeyValue("after_open", "go_custom");
			customizedcast.setPredefinedKeyValue("display_type", "notification");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	        String date = df.format(new Date());
	        JsonObject json = new JsonObject();
	        json.addProperty("date", date);
	        json.addProperty("path", 0);
			customizedcast.setPredefinedKeyValue("custom",json);
			// TODO Set 'production_mode' to 'false' if it's a test device.
			// For how to register a test device, please see the developer doc.
			customizedcast.setPredefinedKeyValue("production_mode", "true");
			customizedcast.send();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void sendIOSCustomizedcast(String telephone, String ticker, String title, String text) {
		IOSCustomizedcast customizedcast = new IOSCustomizedcast();
		try {
			customizedcast.setPredefinedKeyValue("appkey", appkey_ios);
			customizedcast.setPredefinedKeyValue("timestamp", timestamp);
			customizedcast.setPredefinedKeyValue("validation_token", validationToken_ios);
			customizedcast.setPredefinedKeyValue("alias", telephone);
			customizedcast.setPredefinedKeyValue("alias_type", "userId");

			JSONObject apsJson = new JSONObject();
	        apsJson.put("alert", text);
	        customizedcast.setCustomizedField("aps", apsJson);
	        
			customizedcast.setPredefinedKeyValue("production_mode", "false");
			customizedcast.send();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void sendPurchaseAndroidCustomizedcast(String UserId, String ticker, String title, String text) {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast();
		try {
			customizedcast.setPredefinedKeyValue("appkey", appkey_purchaseApp);
			customizedcast.setPredefinedKeyValue("timestamp", timestamp);
			customizedcast.setPredefinedKeyValue("validation_token", validationToken_purchaseApp);
			// TODO Set your alias here, and use comma to split them if there
			// are multiple alias.
			// And if you have many alias, you can also upload a file containing
			// these alias, then
			// use file_id to send customized notification.
			customizedcast.setPredefinedKeyValue("alias", UserId);
			customizedcast.setPredefinedKeyValue("alias_type", "userId");
			customizedcast.setPredefinedKeyValue("ticker", ticker);
			customizedcast.setPredefinedKeyValue("title", title);
			customizedcast.setPredefinedKeyValue("text", text);
			customizedcast.setPredefinedKeyValue("after_open", "go_custom");
			customizedcast.setPredefinedKeyValue("display_type", "notification");
			customizedcast.setPredefinedKeyValue("custom",0);
			// TODO Set 'production_mode' to 'false' if it's a test device.
			// For how to register a test device, please see the developer doc.
			customizedcast.setPredefinedKeyValue("production_mode", "true");
			customizedcast.send();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
